<!DOCTYPE html>
<html>

<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!--
AdSense Management API and Chart Tools week: the source code for Wednesday.

Author silvano.luciani@gmail.com (Silvano Luciani)
-->

  <head>
    <title>AdSense Management API and Chart Tools week: Wednesday</title>
    <script type="text/javascript"
      src='https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"1"}]}'>
    </script>
    <script type="text/javascript" src="conf.js"></script>
    <script type="text/javascript" src="line.js"></script>
    <script type="text/javascript" src="column.js"></script>
    <script type="text/javascript" src="table.js"></script>
    <script type="text/javascript" src="geo.js"></script>
  </head>
  <body>
    <button id="authorize-button" style="visibility: hidden">Authorize</button>
    <div id="line_chart_div"></div>
    <div id="column_chart_div"></div>
    <div id="table_chart_div" style="margin-top: 20px;"></div>
    <div id="geo_chart_div" style="margin-top: 20px;"></div>
    <script type="text/javascript">
    /**
     * Sets the api key and calls checkAuth.
     */
    function handleClientLoad() {
      gapi.client.setApiKey(CONF.apiKey);
      window.setTimeout(checkAuth, 1);
    }

    /**
     * Checks the authorization and calls handleAuthResult once the process
     * is completed.
     */
    function checkAuth() {
      gapi.auth.authorize({client_id: CONF.clientId, scope: CONF.scope,
          immediate: true}, handleAuthResult);
    }

    /**
     * Performs the API request if we have an access token, otherwise shows the
     * authentication button to allow the user to start the flow.
     * Implement makeApiCall in the specific code example to query the API and
     * visualize the data.
     * @param {object} authResult An OAuth 2.0 token and any associated data.
     */
    function handleAuthResult(authResult) {
      var authorizeButton = document.getElementById('authorize-button');
      if (authResult) {
        authorizeButton.style.visibility = 'hidden';
        render();
      } else {
        authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
      }
    }

    /**
     * Handles authentication requests from the authentication button.
     * @param {object} event
     */
    function handleAuthClick(event) {
      gapi.auth.authorize({client_id: CONF.clientId, scope: CONF.scope,
          immediate: false}, handleAuthResult);
    }

    /**
     * Renders the elements of the dashboard.
     */
    function render() {
      var startDate = '2011-01-01';
      var endDate = '2011-12-31';
      var line = new Adsense.Line();
      line.draw(startDate, endDate);
      var column = new Adsense.Column();
      column.draw(startDate, endDate);
      var table = new Adsense.Table();
      table.draw(startDate, endDate);
      var geo = new Adsense.Geo();
      geo.draw(startDate, endDate);
    }
    </script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </body>
</html>

